package com.zenithmind.document.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.Result;
import com.zenithmind.document.pojo.dto.DocumentRatingCreateDTO;
import com.zenithmind.document.pojo.dto.DocumentRatingUpdateDTO;
import com.zenithmind.document.pojo.query.DocumentRatingQueryDTO;
import com.zenithmind.document.pojo.vo.DocumentRatingVO;
import com.zenithmind.document.service.DocumentRatingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import java.util.List;

/**
 * 文档评分控制器
 * 遵循单一职责原则：专注于文档评分相关的HTTP接口
 * 遵循依赖倒置原则：依赖抽象服务接口
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Slf4j
@RestController
@RequestMapping("/api/document/rating")
@RequiredArgsConstructor
@Validated
@Tag(name = "文档评分管理", description = "文档评分相关接口")
public class DocumentRatingController {

    private final DocumentRatingService documentRatingService;

    @PostMapping
    @Operation(summary = "创建评分", description = "创建文档评分")
    public Result<DocumentRatingVO> createRating(@Valid @RequestBody DocumentRatingCreateDTO createDTO) {
        log.info("创建评分请求: documentId={}", createDTO.getDocumentId());
        
        DocumentRatingVO result = documentRatingService.createRating(createDTO);
        
        return Result.success(result);
    }

    @PostMapping("/rate")
    @Operation(summary = "评分文档", description = "用户对文档进行评分")
    public Result<DocumentRatingVO> rateDocument(
            @Parameter(description = "文档ID", required = true) @RequestParam @NotBlank String documentId,
            @Parameter(description = "评分", required = true) @RequestParam Double rating,
            @Parameter(description = "评论") @RequestParam(required = false) String comment) {
        
        log.info("评分文档请求: documentId={}, rating={}", documentId, rating);
        
        String userId = UserContext.getUserId();
        DocumentRatingVO result = documentRatingService.rateDocument(documentId, userId, rating, comment);
        
        return Result.success(result);
    }

    @PutMapping("/update-rating")
    @Operation(summary = "更新用户评分", description = "更新用户对文档的评分")
    public Result<Boolean> updateUserRating(
            @Parameter(description = "文档ID", required = true) @RequestParam @NotBlank String documentId,
            @Parameter(description = "新评分", required = true) @RequestParam Double rating,
            @Parameter(description = "新评论") @RequestParam(required = false) String comment) {
        
        log.info("更新用户评分请求: documentId={}, rating={}", documentId, rating);
        
        String userId = UserContext.getUserId();
        Boolean result = documentRatingService.updateUserRating(documentId, userId, rating, comment);
        
        return Result.success(result);
    }

    @DeleteMapping("/cancel")
    @Operation(summary = "取消用户评分", description = "取消用户对文档的评分")
    public Result<Boolean> cancelUserRating(
            @Parameter(description = "文档ID", required = true) @RequestParam @NotBlank String documentId) {
        
        log.info("取消用户评分请求: documentId={}", documentId);
        
        String userId = UserContext.getUserId();
        Boolean result = documentRatingService.cancelUserRating(documentId, userId);
        
        return Result.success(result);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询评分", description = "分页查询文档评分列表")
    public Result<IPage<DocumentRatingVO>> getRatingPage(@Valid DocumentRatingQueryDTO queryDTO) {
        log.info("分页查询评分请求");
        
        IPage<DocumentRatingVO> result = documentRatingService.getRatingPage(queryDTO);
        
        return Result.success(result);
    }

    @GetMapping("/document/{documentId}")
    @Operation(summary = "获取文档评分列表", description = "获取指定文档的评分列表")
    public Result<List<DocumentRatingVO>> getDocumentRatings(
            @Parameter(description = "文档ID", required = true) @PathVariable @NotBlank String documentId,
            @Parameter(description = "限制数量") @RequestParam(required = false, defaultValue = "20") Integer limit) {
        
        log.info("获取文档评分列表请求: documentId={}", documentId);
        
        List<DocumentRatingVO> result = documentRatingService.getDocumentRatings(documentId, limit);
        
        return Result.success(result);
    }

    @GetMapping("/document/{documentId}/average")
    @Operation(summary = "获取文档平均评分", description = "获取指定文档的平均评分")
    public Result<Double> getDocumentAverageRating(
            @Parameter(description = "文档ID", required = true) @PathVariable @NotBlank String documentId) {
        
        log.info("获取文档平均评分请求: documentId={}", documentId);
        
        Double result = documentRatingService.getDocumentAverageRating(documentId);
        
        return Result.success(result);
    }

    @GetMapping("/document/{documentId}/distribution")
    @Operation(summary = "获取评分分布", description = "获取文档评分分布统计")
    public Result<List<Object>> getRatingDistribution(
            @Parameter(description = "文档ID", required = true) @PathVariable @NotBlank String documentId) {
        
        log.info("获取评分分布请求: documentId={}", documentId);
        
        List<Object> result = documentRatingService.getRatingDistribution(documentId);
        
        return Result.success(result);
    }

    @GetMapping("/top-rated")
    @Operation(summary = "获取热门评分文档", description = "获取评分最高的文档列表")
    public Result<List<Object>> getTopRatedDocuments(
            @Parameter(description = "限制数量") @RequestParam(required = false, defaultValue = "10") Integer limit) {
        
        log.info("获取热门评分文档请求: limit={}", limit);
        
        List<Object> result = documentRatingService.getTopRatedDocuments(limit);
        
        return Result.success(result);
    }

    @GetMapping("/latest")
    @Operation(summary = "获取最新评分", description = "获取最新的评分列表")
    public Result<List<DocumentRatingVO>> getLatestRatings(
            @Parameter(description = "限制数量") @RequestParam(required = false, defaultValue = "10") Integer limit) {
        
        log.info("获取最新评分请求: limit={}", limit);
        
        List<DocumentRatingVO> result = documentRatingService.getLatestRatings(limit);
        
        return Result.success(result);
    }

    @GetMapping("/search")
    @Operation(summary = "搜索评分", description = "根据关键词搜索评分")
    public Result<List<DocumentRatingVO>> searchRatings(
            @Parameter(description = "搜索关键词", required = true) @RequestParam @NotBlank String keyword,
            @Parameter(description = "限制数量") @RequestParam(required = false, defaultValue = "20") Integer limit) {
        
        log.info("搜索评分请求: keyword={}", keyword);
        
        List<DocumentRatingVO> result = documentRatingService.searchRatings(keyword, limit);
        
        return Result.success(result);
    }

    @GetMapping("/comments/{documentId}")
    @Operation(summary = "获取评分评论", description = "获取文档的评分评论")
    public Result<List<DocumentRatingVO>> getRatingComments(
            @Parameter(description = "文档ID", required = true) @PathVariable @NotBlank String documentId,
            @Parameter(description = "是否有评论") @RequestParam(required = false) Boolean hasComment,
            @Parameter(description = "限制数量") @RequestParam(required = false, defaultValue = "20") Integer limit) {
        
        log.info("获取评分评论请求: documentId={}, hasComment={}", documentId, hasComment);
        
        List<DocumentRatingVO> result = documentRatingService.getRatingComments(documentId, hasComment, limit);
        
        return Result.success(result);
    }

    @PostMapping("/{ratingId}/like")
    @Operation(summary = "点赞评分", description = "对评分进行点赞")
    public Result<Boolean> likeRating(
            @Parameter(description = "评分ID", required = true) @PathVariable @NotBlank String ratingId) {
        
        log.info("点赞评分请求: ratingId={}", ratingId);
        
        String userId = UserContext.getUserId();
        Boolean result = documentRatingService.likeRating(ratingId, userId);
        
        return Result.success(result);
    }

    @DeleteMapping("/{ratingId}/like")
    @Operation(summary = "取消点赞评分", description = "取消对评分的点赞")
    public Result<Boolean> unlikeRating(
            @Parameter(description = "评分ID", required = true) @PathVariable @NotBlank String ratingId) {
        
        log.info("取消点赞评分请求: ratingId={}", ratingId);
        
        String userId = UserContext.getUserId();
        Boolean result = documentRatingService.unlikeRating(ratingId, userId);
        
        return Result.success(result);
    }

    @PostMapping("/{ratingId}/report")
    @Operation(summary = "举报评分", description = "举报不当评分")
    public Result<Boolean> reportRating(
            @Parameter(description = "评分ID", required = true) @PathVariable @NotBlank String ratingId,
            @Parameter(description = "举报原因", required = true) @RequestParam @NotBlank String reason) {
        
        log.info("举报评分请求: ratingId={}, reason={}", ratingId, reason);
        
        String userId = UserContext.getUserId();
        Boolean result = documentRatingService.reportRating(ratingId, userId, reason);
        
        return Result.success(result);
    }

    @PostMapping("/{ratingId}/review")
    @Operation(summary = "审核评分", description = "审核评分内容")
    public Result<Boolean> reviewRating(
            @Parameter(description = "评分ID", required = true) @PathVariable @NotBlank String ratingId,
            @Parameter(description = "审核状态", required = true) @RequestParam Integer status) {
        
        log.info("审核评分请求: ratingId={}, status={}", ratingId, status);
        
        String reviewerId = UserContext.getUserId();
        Boolean result = documentRatingService.reviewRating(ratingId, status, reviewerId);
        
        return Result.success(result);
    }

    @GetMapping("/pending-review")
    @Operation(summary = "获取待审核评分", description = "获取待审核的评分列表")
    public Result<List<DocumentRatingVO>> getPendingReviewRatings(
            @Parameter(description = "限制数量") @RequestParam(required = false, defaultValue = "20") Integer limit) {
        
        log.info("获取待审核评分请求: limit={}", limit);
        
        List<DocumentRatingVO> result = documentRatingService.getPendingReviewRatings(limit);
        
        return Result.success(result);
    }

    @PostMapping("/sync/{documentId}")
    @Operation(summary = "同步评分统计", description = "同步指定文档的评分统计")
    public Result<Boolean> syncRatingStatistics(
            @Parameter(description = "文档ID", required = true) @PathVariable @NotBlank String documentId) {
        
        log.info("同步评分统计请求: documentId={}", documentId);
        
        Boolean result = documentRatingService.syncRatingStatistics(documentId);
        
        return Result.success(result);
    }

    @PostMapping("/cleanup")
    @Operation(summary = "清理垃圾评分", description = "清理垃圾评分数据")
    public Result<Integer> cleanupSpamRatings() {
        log.info("清理垃圾评分请求");
        
        Integer result = documentRatingService.cleanupSpamRatings();
        
        return Result.success(result);
    }
}
